<?php

namespace app\admin\model\litestore;

use think\Model;

class Posid extends Model
{
    // 表名
    protected $name = 'litestore_posid';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [

    ];


    public function shop_posids_id($shopid)
    {
        $ids = db('litestore_posid_mid')->where('lid',$shopid)->select();
        $tids = [];
        foreach ($ids as $value) {
            $tids[] = $value['tid'];
        }
        return $tids;
    }

    /**
     * @param $shopid
     * @param $posids
     * 新增所属推荐位
     */
    public function add_posids($shopid,$posids)
    {
        foreach ($posids as $value) {
            db('litestore_posid_mid')->insert(['lid'=>$shopid,'tid'=>$value]);
        }
    }

    /**
     * @param $shopid
     * @param $posids
     * 修改商户推荐位
     */
    public function edit_posids($shopid, $posids)
    {
        $ids = $this->shop_posids_id($shopid);

        foreach ($posids as $value) {
            if(!in_array($value,$ids) && $value != 0){
                db('litestore_posid_mid')->insert(['lid'=>$shopid,'tid'=>$value]);
            }
        }

        foreach ($ids as $value) {
            if(!in_array($value,$posids)){
                db('litestore_posid_mid')->where(['lid'=>$shopid,'tid'=>$value])->delete();
            }
        }


    }

    







}
